<?php

/**
 * @author      :   QuangLT
 * @name        :   NewsModel
 * @copyright   :   Fpt Online
 * @todo        :   News model
 */
class Statictis {

    protected static $_instance = null;

    /**
     * Constructor of class
     * we don't permit an explicit call of the constructor! (like $v = new Singleton())
     */
    protected function __construct() {
        
    }

    /**
     * Get singletom instance
     * @return <object>
     */
    public final static function getInstance() {
        //Check instance
        if (is_null(self::$_instance)) {
            self::$_instance = new self();
        }

        //Return instance
        return self::$_instance;
    }

    /**
     * Get top view article
     * @param type $intSiteID
     * @param type $intArticleType
     * @param type $intTotal
     * @return array List ID of article
     */
    public function getTopViewArticle($intSiteID = SITE_ID, $intTotal = 5, $intArticleType = 1) {
        $arrTopView = array();
        $config = Fpt_Global::getApplicationIni();

        // Get nosql instance to get list top view article
        $staticNosql = Fpt_Business_Statictis::getInstance($config['database']['nosql']);

        // Get instance of news to get detail article
        $instanceNews = News::getInstance();

        $arrArticle = $staticNosql->getTopViewArticle($intSiteID, $intTotal);

        /* //////////////////////////REMOVE///////////////////////////// */
        // For testing, get list from DB
        if (empty($arrArticle)) {
            // Get instance DB
            $statisticMysql = Fpt_Business_Statictis::getInstance($config['database']['default']);

            // Get top comment article
            $arrTmp = $statisticMysql->getTopViewArticle($intSiteID, $intTotal, $intArticleType);

            // Result from sql in format: array([0] => Array([article_id] => ID)
            if (!empty($arrTmp)) {
                foreach ($arrTmp as $arrItem) {
                    $arrArticle[] = $arrItem['article_id'];
                }
            }
        }
        /* //////////////////////////REMOVE///////////////////////////// */

        if (!empty($arrArticle)) {
            // Loop list, to get article detail
            foreach ($arrArticle as $intArticleID) {
                $arrTmp = $instanceNews->getDetailArticle($intArticleID);
                if( !empty ($arrTmp))
                $arrTopView[] = $arrTmp;
            }
        }

        return $arrTopView;
    }

    /**
     * Get top comment article
     * @param type $intSiteID
     * @param type $intTotal 
     * @return array List ID of article
     */
    public function getTopCommentArticle($intSiteID = SITE_ID, $intTotal = 5) {

        $arrTopView = array();
        $config = Fpt_Global::getApplicationIni();

        // Get nosql instance to get list top view article
        $staticNosql = Fpt_Business_Statictis::getInstance($config['database']['nosql']);

        // Get instance of news to get detail article
        $instanceNews = News::getInstance();

        $arrArticle = $staticNosql->getTopCommentArticle($intSiteID, $intTotal);

        /* //////////////////////////REMOVE///////////////////////////// */
        // For testing, get list from DB
        if (empty($arrTopViewArticle)) {
            // Get instance DB
            $statisticMysql = Fpt_Business_Statictis::getInstance($config['database']['default']);

            // Get top comment article
            $arrTmp = $statisticMysql->getTopCommentArticle($intSiteID, $intTotal);
             // Result from sql in format: array([0] => Array([article_id] => ID)
            if (!empty($arrTmp)) {
                foreach ($arrTmp as $arrItem) {
                    $arrArticle[] = $arrItem['article_id'];
                }
            }

        }
        /* //////////////////////////REMOVE///////////////////////////// */

        // Loop list, to get article detail
        foreach ($arrArticle as $intArticleID) {
            $arrTopView[] = $instanceNews->getDetailArticle($intArticleID);
        }

        return $arrTopView;
    }

    /**
     * get Num view of Article
     * @author TriNT
     * @param <INT> $p_articleID
     * @return <array>
     */
    public function getNumViewArticle($p_articleID) {
        $config = Fpt_Global::getApplicationIni();
        //get statistic mysql instance
        $statisticMysql = Fpt_Business_Statictis::getInstance($config['database']['default']);
        return $statisticMysql->getNumViewArticle($p_articleID);
    }

    /**
     * Get number comment article
     * @param array $params
     * @return array
     * @author  : phongtx@fpt.net
     */
    public function getNumCommentArticle($arrParams) {
        try {
            $config = Fpt_Global::getApplicationIni();
            //get statistic mysql instance
            $statisticMysql = Fpt_Business_Statictis::getInstance($config['database']['default']);
            $arrNumCommentArticle = $statisticMysql->getNumCommentArticle($arrParams);
            return $arrNumCommentArticle;
        } catch (Zend_Exception $ex) {
            throw new Zend_Exception($ex->getMessage(), 1);
            return false;
        }
    }

}